package com.example.administrator.player;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.util.List;

public class MusicService extends Service {
    private MediaPlayer player;
    private int flag;
    private String filename;
    private List<String> sList;
    private String path = "/sdcard/Music/";
    private int progress;
    public MusicService() {
    }


    @Override
    public void onCreate() {
        super.onCreate();
        sList = MainActivity.getSongsList();
        player = new MediaPlayer();
        player.reset();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int action = intent.getIntExtra("action",0);
        int pro = intent.getIntExtra("progress",0);
        flag = MainActivity.flag;
        switch(action){
            case MainActivity.PLAY:
                filename = sList.get(flag);
                try {
                    player.setDataSource(path + filename);
                    player.prepare();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                player.start();
            break;
            case MainActivity.PAUSE:
                player.pause();
            break;
            case MainActivity.REPLAY :
                player.seekTo(0);
            break;
            case MainActivity.LASTORNEXT :
                player.reset();
                filename = sList.get(flag);
                try {
                    player.setDataSource(path + filename);
                    player.prepare();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                player.start();
                break;
            case MainActivity.JUMP :
                try {
                    player.setDataSource(path+filename);
                    player.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                player.seekTo(pro);
                player.start();
                break;
            default:break;
        }
        return super.onStartCommand(intent,flags,startId);
    }

    public class MyBinder extends Binder {


        public void getProgress(){

            new Thread(){
                @Override
                public void run() {
                    for(int i = 0; i <= (player.getDuration()/200+1);i++){
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        progress = player.getCurrentPosition();
                        MainActivity.seekbar.setProgress(progress);
                    }
                }
            }.start();
        }
        public int getDuration(){

            return player.getDuration();
        }

    }
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
